﻿using NetworkLib.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace PBO.Network
{
    public static class StringUtils
    {
        public static T DeserializeObject<T>(string s)
        {
            //return JsonConvert.DeserializeObject<T>(s);
            return XmlDeserialize<T>(s);
        }

        public static string SerializeObject<T>(this T obj)
        {
            //return JsonConvert.SerializeObject(obj);
            return XmlSerialize<T>(obj);
        }

        private static T XmlDeserialize<T>(string s)
        {
            T obj;
            using (var sr = new StringReader(s))
            {
                var xs = new XmlSerializer(typeof(T));
                obj = (T)xs.Deserialize(sr);
            }
            return obj;
        }

        private static string XmlSerialize<T>(T obj)
        {
            var xs = new XmlSerializer(typeof(T));
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);
            xs.Serialize(sw, obj);
            return sb.ToString();
        }
    }
}
